package hrms.controller;

import hrms.entity.Account;
import hrms.entity.Article;
import hrms.entity.Result;
import hrms.service.AccountService;
import hrms.service.ArticleService;
import hrms.service.SortService;
import hrms.util.JWTUtil;
import hrms.util.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private SortService sortService;

    public String getAccountId(HttpServletRequest request){
        String token = request.getHeader("token");
        String id = JWTUtil.getId(token);
        return id;
    }

    @PostMapping("/addArticle")
    public Result addArticle(@RequestBody Article article, HttpServletRequest request){
//        System.out.println(article.getTitle() + article.getContent());
        article.setAccount(accountService.findAccountById(Integer.parseInt(getAccountId(request))));
        article.setDate(new Date());
        article.setStatus(1);
        article.setViews((long) 1);
        article.setSort(sortService.findSortById(article.getSort().getId()));
        articleService.addArticle(article);
        return Result.success();
    }

    @PostMapping("/editArticle")
    public Result editArticle(@RequestBody Article article){
        Article articleForBase = articleService.findArticleById(article.getId());
        // 更新文章的标题，内容和时间
        articleForBase.setTitle(article.getTitle());
        articleForBase.setContent(article.getContent());
        articleForBase.setDate(new Date());
        articleService.editArticle(articleForBase);
        return Result.success();
    }

    @GetMapping("/findSingleArticle")
    public Result findSingleArticle(int id){
        Article article = articleService.findArticleById(id);
        if(article == null){
            return Result.fail().error("找不到该文档");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("article", article);
        return Result.success().addMap(map);
    }



    @GetMapping("/findAllArticles")
    public Result findAllArticles(){
        List<Article> articles = articleService.findAllArticles();
        if(articles == null){
            return Result.fail().error("找不到任何文档");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("articles", articles);
        return Result.success().addMap(map);
    }

    @GetMapping("/deleteSingleArticle")
    public Result deleteSingleArticle(int id){
        Article article = articleService.findArticleById(id);
        if(article == null){
            return Result.fail().error("找不到该文档，删除失败");
        }
        articleService.deleteArticle(id);
        return Result.success();
    }

    @GetMapping(value="/findPage")
    public Object findPage(@RequestParam(defaultValue = "1") int page) {
        PageRequest pageRequest = new PageRequest();
        pageRequest.setPageNum(page);
        pageRequest.setPageSize(5);
        return articleService.findPage(pageRequest);
    }


}
